package com.jcli.jb51_net.article._获取屏幕中颜色;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ColorPicker extends JFrame {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 250;
    private JPanel mainPanel;
    private JPanel colorHistoryPanel;
    private JComboBox<String> colorModeCombo;
    private JTextField colorInputField;
    private JButton copyButton;

    private Robot robot;
    private List<Color> colorHistory;

    private JPanel thumbnailPanel;



    public ColorPicker() {
        setSize(WIDTH, HEIGHT);
        setTitle("Color Picker");
        setLayout(new BorderLayout());

        colorHistory = new ArrayList<>();

        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        // Add colorModeCombo, colorInputField, and copyButton to a control panel
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        colorModeCombo = new JComboBox<>(new String[]{"RGB", "Hex"});
        controlPanel.add(colorModeCombo);

        colorInputField = new JTextField(10);
        controlPanel.add(colorInputField);

        copyButton = new JButton("Copy");
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String colorText = colorInputField.getText();
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(new StringSelection(colorText), null);
            }
        });
        controlPanel.add(copyButton);

        mainPanel.add(controlPanel, BorderLayout.NORTH);

        // Add color history panel to the center
        colorHistoryPanel = new JPanel();
        mainPanel.add(colorHistoryPanel, BorderLayout.CENTER);

        add(mainPanel, BorderLayout.CENTER);

        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                recordColor();
            }
        }, 100, 100);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                displayColorInfo(e.getX(), e.getY());
            }
        });

        // Create thumbnail panel for displaying mouse location surroundings
        thumbnailPanel = new JPanel();
        thumbnailPanel.setPreferredSize(new Dimension(150, 150));
        mainPanel.add(thumbnailPanel, BorderLayout.EAST);

        addKeyListener(new KeyAdapter() {
            boolean altPressed = false;

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ALT) {
                    altPressed = true;
                } else if (altPressed && e.getKeyChar() == 'x') {
                    displayThumbnailAndColorInfo();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ALT) {
                    altPressed = false;
                }
            }
        });
    }

   /**
   显示缩略图和颜色信息
    */
    private void displayThumbnailAndColorInfo() {
        Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
        int mouseX = mouseLocation.x;
        int mouseY = mouseLocation.y;

        Rectangle area = new Rectangle(mouseX - 75, mouseY - 75, 150, 150);
        BufferedImage thumbnail = robot.createScreenCapture(area);

        ImageIcon thumbnailIcon = new ImageIcon(thumbnail.getScaledInstance(150, 150, Image.SCALE_SMOOTH));
        JLabel thumbnailLabel = new JLabel(thumbnailIcon);

        thumbnailPanel.removeAll();
        thumbnailPanel.add(thumbnailLabel);
        thumbnailPanel.revalidate();
        thumbnailPanel.repaint();

        displayColorInfo(mouseX, mouseY);
    }


    private void displayColorInfo(int x, int y) {
        Point point = new Point(x, y);
        Color pixel = robot.getPixelColor(point.x, point.y);
        String colorText = getColorText(pixel);

        colorInputField.setText(colorText);
    }

    private String getColorText(Color color) {
        String colorText;
        // Get color text based on selected color mode
        // You can implement conversion to different color modes (RGB, Hex, etc.) here
        if (colorModeCombo.getSelectedItem().equals("RGB")) {
            colorText = String.format("RGB: %d, %d, %d", color.getRed(), color.getGreen(), color.getBlue());
        } else {
            // Convert color to Hex format
            colorText = String.format("Hex: #%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue());
        }
        return colorText;
    }

    private void recordColor() {
        Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
        Color pixelColor = robot.getPixelColor(mouseLocation.x, mouseLocation.y);
        colorHistory.add(pixelColor);

        refreshColorHistory();
    }

    private void refreshColorHistory() {
        colorHistoryPanel.removeAll();

        for (int i = 0; i < colorHistory.size(); i++) {
            JPanel colorPanel = new JPanel();
            colorPanel.setPreferredSize(new Dimension(20, 20));
            colorPanel.setBackground(colorHistory.get(i));
            colorHistoryPanel.add(colorPanel);
        }

        colorHistoryPanel.revalidate();
        colorHistoryPanel.repaint();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            ColorPicker colorPicker = new ColorPicker();
            colorPicker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            colorPicker.setVisible(true);
            colorPicker.setFocusable(true);
            colorPicker.requestFocus();
        });
    }
}
